package com.tb.ClassSchedule;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.ActionBar;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ScrollView;

/**
 * Created by guich on 2016/8/23.
 */
public class WeekListButton extends Button implements View.OnClickListener{

    private PopupWindow  weekList;
    private int          selectedWeek;
    private int          currentWeek;
    private ScrollView   scrollView;
    private LinearLayout weekListView;

    public WeekListButton(Context context, AttributeSet attrs) {
        super(context, attrs);

        setBackgroundResource(R.drawable.button_animation);
        setGravity(Gravity.CENTER);
        setText("第一周");
        setTextColor(Color.WHITE);
        setTextSize(20);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                showList();
            }
        });


        loadSrc();

    }

    private void showList() {
        selectedWeek = MainActivity.main.selectedWeek;
        currentWeek = MainActivity.main.currentWeek;
        drawWeekListView();
        scrollView.post(new Runnable() {
            @Override
            public void run() {
                scrollView.scrollTo(0,(int)((selectedWeek - 1.5) * weekListView.getHeight() / 22));
            }
        });
//        weekList.showAsDropDown(weekListButton,(weekListButton.getWidth() - weekList.getWidth()) / 2,0);
        weekList.showAtLocation(this,Gravity.TOP,0,this.getHeight() + MainActivity.statusBarHeight);

    }

    private void drawWeekListView() {
        weekListView.removeAllViews();
        for(int i = 0 ; i < 22 ; i++){
            Button button = new Button(getContext());
            String       str    = "";
            switch (i){
                case 0:
                    str = "未开学";
                    break;
                case 21:
                    str = "结束啦";
                    break;
                default:
                    str = "第" + i + "周";

            }
            if( i == currentWeek){
                str += "(本周)";
            }

            button.setText(str);
            button.setId(i);
            button.setOnClickListener(this);
            if( i == selectedWeek ){
                button.setBackgroundColor(Color.argb(200,255,255,255));
            }else{
                button.setBackgroundResource(R.drawable.button_animation);
            }

            weekListView.addView(button);
        }


    }

    private void loadSrc() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.week_list_layout,null);
        weekList = new PopupWindow(view, ActionBar.LayoutParams.WRAP_CONTENT,ActionBar.LayoutParams.WRAP_CONTENT,true);
        weekList.setBackgroundDrawable(new BitmapDrawable());
        weekList.setOutsideTouchable(true);

        weekListView = (LinearLayout) view.findViewById(R.id.week_list_view);

        scrollView = (ScrollView) view.findViewById(R.id.week_list_scrollview);

        Button setCurrentWeekButton = (Button) view.findViewById(R.id.set_current_week);
        setCurrentWeekButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TimeManager.setCurrentWeekDialog();
                weekList.dismiss();
            }
        });
    }


    @Override
    public void onClick(View view) {
        MainActivity.main.selectedWeek = view.getId();
        Log.d("tb", String.valueOf(MainActivity.main.selectedWeek));
        weekList.dismiss();
        MainActivity.main.draw();
    }
}
